package com.crcode.crm.utils;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
/**
 * solr索引工具类
 * @author xzq
 * @date 创建时间:2018:04:02 16:30:11 
 */
public class SolrIndexUtil {
	public static void main(String[] args){
		System.out.println("org/apache/http/entity/mime/content/ContentBody");
	}
	/**
	 * 报错jar包路径测试
	 * 
	 * @param klass
	 * @return
	 * @author xzq
	 */
	public static String getClassResource(Class<?> klass) {
		return klass.getClassLoader().getResource(klass.getName().replace('.', '/') + ".class").toString();
	}
	/**
	 * 从文件创建索引 <功能详细描述>
	 * @param fileName
	 * @param solrId---solr索引文件中的唯一值
	 * @param archiveId---档案主表唯一值---用于反推档案信息
	 * @see [类、类#方法、类#成员]
	 */
	public void indexFilesSolrCell(String fileName, String solrId,String archiveId,
			String tableName,String xmmc,String cxmmc,String cid,String zid,
			String fname,String path,String xmfwbm) throws IOException, SolrServerException {
		try {
			String urlString = new PropertiesUtil().getTerm("solr.properties", "solrIndexForPDF");
			SolrServer solr = new HttpSolrServer(urlString);
			ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
			// 文件类型
			String contentType=getFileContentType(fileName);
			up.addFile(new File(fileName), contentType);
			// 索引唯一值
			up.setParam("literal.id", solrId);
			up.setParam("uprefix", "attr_");
			up.setParam("literal.archiveid", archiveId);
			up.setParam("literal.tablename", tableName);
			up.setParam("literal.xmmc", xmmc);
			up.setParam("literal.cxmmc", cxmmc);
			up.setParam("literal.cid", cid);
			up.setParam("literal.zid", zid);
			up.setParam("literal.fname", fname);
			up.setParam("literal.path", path);
			up.setParam("literal.xmfwbm", xmfwbm);
			// 文件内容
			up.setParam("fmap.content", "attr_content");
			up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
			solr.request(up);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 根据query条件删除文件类型索引
	 * @return
	 */
	public void delPDFIndexs(String query) {
		try {
			PropertiesUtil p = new PropertiesUtil();
			// 获取properties配置文件数据
			String solrurl = p.getTerm("solr.properties", "solrIndexForPDF");
			HttpSolrServer solr = new HttpSolrServer(solrurl);
			solr.deleteByQuery(query);//"onlyId:[0 TO ~]"
			solr.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 合并索引
	 * @param solrList-----要合并的索引
	 * @param solrs---合并后的索引
	 * @return
	 * @throws Exception
	 * @author xzq
	 */
	public boolean mergeSolrIndexes(List<String> solrList, String solrs) throws Exception {
		PropertiesUtil p = new PropertiesUtil();
		// 获取properties配置文件数据
		String solrurl = p.getTerm("solr.properties", "solr");
		String urlStr = solrurl + "/admin/cores?action=mergeindexes&core=" + solrs;
		for (String str : solrList) {
			urlStr += "&srcCore=" + str;
		}
		// 调用外部程序
		URL url = new URL(urlStr);
		URLConnection urlConnection = url.openConnection();
		HttpURLConnection conn = (HttpURLConnection) urlConnection;
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setRequestProperty("Content-type", "application/json;charset=utf-8");
		conn.setRequestMethod("POST");
		conn.connect();
		int responseCode = conn.getResponseCode();
		if (responseCode == 200) {
			// 合并成功
			String urls = solrurl+"/" + solrs;
			HttpSolrServer solr = new HttpSolrServer(urls);
			// 合并成功，提交索引
			solr.commit();
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 根据query条件删除索引
	 * @return
	 * @throws IOException
	 * @throws SolrServerException
	 * @author xzq
	 */
	public boolean delIndexs(String query) throws SolrServerException, IOException {
		try {
			PropertiesUtil p = new PropertiesUtil();
			// 获取properties配置文件数据
			String solrurl = p.getTerm("solr.properties", "solrcore");
			HttpSolrServer solr = new HttpSolrServer(solrurl);
			solr.deleteByQuery(query);// "onlyId:[0 TO ~]"
			solr.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 根据文件名获取文件的ContentType类型
	 * @param filename
	 * @return
	 */
	public static String getFileContentType(String filename) {
		String contentType = "";
		String prefix = filename.substring(filename.lastIndexOf(".") + 1);
		if (prefix.equals("xlsx")) {
			contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
		} else if (prefix.equals("pdf")) {
			contentType = "application/pdf";
		} else if (prefix.equals("doc")) {
			contentType = "application/msword";
		} else if (prefix.equals("txt")) {
			contentType = "text/plain";
		} else if (prefix.equals("xls")) {
			contentType = "application/vnd.ms-excel";
		} else if (prefix.equals("docx")) {
			contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
		} else if (prefix.equals("ppt")) {
			contentType = "application/vnd.ms-powerpoint";
		} else if (prefix.equals("pptx")) {
			contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
		} else {
			contentType = "othertype";
		}
		return contentType;
	}
}
